package com.salts.service.Impl;

import com.salts.Dao.ClothMapper;
import com.salts.Dao.OrderDetailMapper;
import com.salts.Dao.OrderInfoMapper;
import com.salts.domain.dto.ClothDto;
import com.salts.domain.po.OrderDetail;
import com.salts.domain.po.OrderInfo;
import com.salts.domain.po.UserAddress;
import com.salts.domain.po.UserInfo;
import com.salts.domain.vo.ClothVo;
import com.salts.domain.vo.DisplayClothVo;
import com.salts.domain.vo.DisplayOrderVo;
import com.salts.domain.vo.OrderVo;
import com.salts.service.OrderFunctionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class OrderFunctionServiceImpl implements OrderFunctionService {
    @Autowired
    OrderInfoMapper orderInfoMapper;
    @Autowired
    OrderDetailMapper orderDetailMapper;
    @Autowired
    ClothMapper clothMapper;
    @Override
    public OrderVo getOrderInfo(UserInfo userInfo, Integer orderId) {
        if (userInfo.getId()==0){
            return null;
        }
        int userId = userInfo.getId();
        OrderVo orderVo = new OrderVo();
        OrderInfo orderInfo=orderInfoMapper.getOrderInfo(orderId,userId);
        List<OrderDetail> orderDetailInfos=orderDetailMapper.getOrderDetailInfo(Collections.singletonList(orderId));
        orderVo.setOrderInfo(orderInfo);
        orderVo.setOrderDetails(orderDetailInfos);
        return orderVo;
    }

    /**
     * Key为order_no,value为DisplayOrderVo
     *
     * @param userInfo
     * @param orderState
     * @return
     */
    @Override
    public HashMap<String, List<DisplayOrderVo>> getOrderList(UserInfo userInfo, Integer orderState) {
        int id = userInfo.getId();
        //准备容器
        HashMap<String, DisplayOrderVo> returnMap = new HashMap<>();
        List<DisplayOrderVo> values = null;
        //查询对应的订单DisplayVO
        List<OrderInfo> orderInfoByUserId = orderInfoMapper.getOrderInfoByUserId(id,orderState);
        List<Integer> orderIdList = orderInfoByUserId.stream().map(OrderInfo::getOrderId).collect(Collectors.toList());
        List<String> orderNoList = orderInfoByUserId.stream().map(OrderInfo::getOrderNo).collect(Collectors.toList());
        List<OrderDetail> orderDetailInfo = orderDetailMapper.getOrderDetailInfo(orderIdList);
        // 遍历订单明细，将订单号作为键，将对应的 OrderDetail 对象列表放入值中
        //一条明细对应一个DisplayOrderVo
        List<Integer> clothIdList = orderDetailInfo.stream().map(OrderDetail::getClothId).distinct().collect(Collectors.toList());
        List<ClothDto> displayClothVo=clothMapper.getDisplayClothDto(clothIdList);
        HashMap<Integer, ClothDto> clothMap = displayClothVo.stream().collect(Collectors.toMap(ClothDto::getClothId, vo -> vo, (existing, replacement) -> existing, HashMap::new));
        HashMap<String, List<DisplayOrderVo>> OrderVo = new HashMap<>();
        for (OrderDetail orderDetail : orderDetailInfo) {
            String orderNo = orderDetail.getOrderNo();
            DisplayOrderVo displayOrderVo = new DisplayOrderVo();
            ClothDto clothVo = clothMap.get(orderDetail.getClothId());
            // 获取 orderDetail 的 skuPayPrice 和 purchaseNum
            BigDecimal skuPayPrice = orderDetail.getSkuPayPrice();
            int purchaseNum = orderDetail.getPurchaseNum();
            // 使用 BigDecimal 进行精确计算
            BigDecimal thisPriceCount = skuPayPrice.multiply(BigDecimal.valueOf(purchaseNum));
            displayOrderVo.setId(orderDetail.getId());
            displayOrderVo.setNumber(purchaseNum);
            displayOrderVo.setSkuPayPrice(skuPayPrice);
            displayOrderVo.setThisPriceCount(thisPriceCount);
            displayOrderVo.setColor(clothVo.getColor());
            displayOrderVo.setSpec(clothVo.getSpec());
            displayOrderVo.setImgUrl(clothVo.getImgUrl());
            displayOrderVo.setClothName(clothVo.getClothName());
            displayOrderVo.setClothId(clothVo.getClothId());
            OrderVo.computeIfAbsent(orderNo, k -> new ArrayList<>()).add(displayOrderVo);//这句话整理categoryOne,把categoryTwo收集成List返回
        }
        //1订单对应多displayOrderVo
        return OrderVo;
    }

    @Override
    public Boolean updateOrderState(Integer orderId, Integer state) {
        return orderInfoMapper.updaeOrderState(state,orderId);
    }

    @Override
    public Boolean updateOrderAddress(Integer orderId, UserAddress userAddress) {
        String address = userAddress.getAddress();
        String provinceName = userAddress.getProvinceName();
        String cityName = userAddress.getCityName();
        String areaName = userAddress.getAreaName();
        return orderInfoMapper.updateOrderAddress(address,provinceName,cityName,areaName,orderId);
    }

    @Override
    public boolean orderRefund(Integer userId, Integer orderId) {
        return orderInfoMapper.orderRefund(orderId);
    }
}
